package com.zking;

import java.util.stream.IntStream;

public class SteamDemo1 {
    public static void main(String[] args) {
        int[] nums={1,2,3};
        //外部迭代
        int sum=0;
        for (int num : nums) {
            sum+=num;
        }
        System.out.println(sum);

        //使用steam内部迭代
        int sum1 = IntStream.of(nums).sum();
        //map是中间操作
        //sum是中止操作
        int sum2 = IntStream.of(nums).map(i -> i * 2).sum();
        System.out.println(sum1);
        System.out.println(sum2);
        int sum3 = IntStream.of(nums).map(SteamDemo1::doubleNum).sum();
        System.out.println(sum3);
        System.out.println("惰性求值就是终止了没用调用的情况下，中间操作不会执行了");
        System.out.println(IntStream.of(nums).map(SteamDemo1::doubleNum));
    }
    public static int doubleNum(int i){
        System.out.println("执行了乘以2");
        return i*2;
    }
}
